package chapter2;

import com.google.gwt.core.ext.BadPropertyValueException;
import com.google.gwt.core.ext.Generator;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.PropertyOracle;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;

public class WelcomeGenerator extends Generator {

	@Override
	public String generate(TreeLogger logger, GeneratorContext context,
			String typeName) throws UnableToCompleteException {
		try {
			if (typeName.equals("chapter2.client.WelcomeImpl")) {
				PropertyOracle propOracle = context.getPropertyOracle();
				//String userAgent=propOracle.getConfigurationProperty("user.agent").getName();
				String userAgent = propOracle.getSelectionProperty(logger, "user.agent").getCurrentValue();
				System.out.println("userAgent:"+userAgent);
				if (userAgent.equals("ie11")) {
					return "chapter2.client.WelcomeImplIE";
				}
				else if (userAgent.equals("gecko") || userAgent.equals("gecko1_8")) {
					return "chapter2.client.WelcomeImplFirefox";
				}
				else if (userAgent.equals("safari") || userAgent.equals("safari")) {
					return "chapter2.client.WelcomeImplSafari";
				}
				else {
					return "chapter2.client.WelcomeImpl";
				}
			}
			else {
				throw new UnableToCompleteException();
			}
		} catch (BadPropertyValueException e) {
			e.printStackTrace();
		}
		return null;
	}

}
